<?php


namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\User as UserModel;
use think\Db;

/**
 * 用户模型
 * @package app\yunda\admin
 */
class User extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        //$map['user_type']=0;//是否员工
        $order = $this->getOrder();
        $btn=[//设为员工
            'title' => '设为员工',
            'icon'  => 'fa fa-fw fa-users',
            'class' => 'btn btn-primary ajax-post',//选中多个数据必须是post请求
            'href'  => url('staff')
        ];
        $data = Db::table('dp_yd_user',true)
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['place']=$item['home'].$item['address'];
                if($item['member_type']){
                    $item['member_type']=config('member_level')[$item['member_type']];
                }else{
                    $item['member_type']='普通';
                }
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_user')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
//                ['name','姓名'],
                ['openid','Openid'],
                ['nickname','昵称'],
                ['head_img','头像','img_url'],
                ['phone','电话'],
                ['money','余额'],
                ['score','积分'],
                ['member_type','会员等级'],
                ['member_time','会员申请时间','datetime'],
                ['award_time','抽奖次数'],
                ['user_type','是否员工','status','',['否:primary','是:danger']],
//                ['sex','性别'],
//                ['birthday','生日'],
//                ['card','身份证'],
//                ['email','邮箱'],
//                ['place','地址'],
//                ['post_code','邮编'],
                ['create_time','创建时间','datetime'],
//                ['right_button', '操作', 'btn']
            ])
            //->addTopButton('add', [],[])
            ->addTopButton('staff',$btn)
//            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
//            ->addFilter(['name'])
            ->setSearch(['id' => 'ID', 'name' => '姓名','phone'=>'电话'])
            ->fetch();
    }
    public function add()
    {
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'User');
            if($result!==true) $this->error($result);
            // 第三部 存储
            UserModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
               // ['text', 'title', '标题'],

            ])
            ->fetch();
    }
    public function edit($id=null)
    {
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'User');
            if($result!==true) $this->error($result);
            UserModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = UserModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
               // ['text', 'title', '标题'],
            ])
            ->setFormData($data)
            ->fetch();
    }

    //设为员工
    public function staff($ids)
    {
        foreach ($ids as $v){
            $row=UserModel::get($v);
            if($row['user_type']==1) $this->error('ID为'.$row['id'].'用户已经是员工');
            $row->update(['user_type'=>1,'id'=>$row['id']]);
        }
        $this->success('操作成功');
    }


}